<template>
    <div style="padding-bottom: 10px; border:1px solid rgb(228, 228, 228); border-radius: 10px;">
        <el-header class="head">
            {{ compile == 1 ? "团课课程添加" : "团课课表编辑" }}
            <router-link to="/GroupScheduleView">
                <el-button round>&lt; 返回</el-button>
            </router-link>
        </el-header>
        <div class="content">
            <el-form ref="ruleForm" v-model="tableData" label-width="120px" class="demo-ruleForm">

                <el-form-item label="课程名称：" required>
                    <el-input v-model="tableData.name"></el-input>
                </el-form-item>

                <el-form-item label="课程图片：" v-model="tableData.pic">
                    <div class="kechengbox">
                        <div class="">
                            <el-upload class="avatar-uploader" action="/api/gym-user-api/admin/upload/uploadImageByMinio"
                            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                            name="file" :headers="{ 'admin-token': token }">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                            <el-icon v-else class="avatar-uploader-icon">
                                <Plus />
                            </el-icon>
                        </el-upload>

                        </div>
                        <p class="Deleteimg">删除</p>

                    </div>
                </el-form-item>

                <el-form-item label="课程时长：" required>
                    <el-input v-model="tableData.time" type="number"></el-input>
                </el-form-item>

                <el-form-item label="最多上课人数：" required>
                    <el-input v-model="tableData.mastClassNum" type="number"></el-input>
                </el-form-item>

                <el-form-item label="教练：" prop="coachName" v-model="tableData.coachName">
                    <el-select v-model="tableData.coachId" class="m-2" placeholder="请选择教练">
                        <el-option v-for="item in coachList" :key="item.name" :label="item.name" :value="item.adminId" />
                    </el-select>
                </el-form-item>

                <el-form-item label="课时费：" prop="price">
                    <el-input v-model="tableData.courseMoney"></el-input>
                </el-form-item>


                <el-form-item label="课程介绍" prop="description">
                    <el-input v-model="tableData.introduce" type="textarea"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="NewEdit">提交</el-button>
                   
                    <el-button>重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import { ref,reactive } from "vue"
import { GroupHooks, Groups } from '../hooks/GroupHooks';
import { ElMessage } from 'element-plus';
import { Plus } from "@element-plus/icons-vue";
let token = sessionStorage.getItem("token");
// console.log(token)

const _GroupHooks: Groups = GroupHooks()



const tableData = reactive({
    coachName: 0,
    courseMoney: "",
    coursePlanType: 0,
    introduce: "",
    mastClassNum: 0,
    name: "",
    pic: "",
    sort: 0,
    time: 0,
})

const route = useRoute()
const compile = route.query.compile
const id = route.query.id
_GroupHooks.getGroupCompile(id, compileList)
function compileList(data: any) {
    Object.assign(tableData,data)

}

//添加事件
function NewEdit() {
    if (compile == '1') {
        _GroupHooks.getGroupCourseAdded({ ...tableData })
    } else {
        _GroupHooks.getGroupCourseEditor(tableData, id)
    }

}
//教练接口
_GroupHooks.getCoachList(coachData)
const coachList = ref<Array<any>>([])
function coachData(data: any) {
    coachList.value = data
}



const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
    console.log("相应,",response.data)
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
    tableData.pic=response.data
    console.log("pic",tableData.pic)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}



</script>

<style scoped>
:deep(.tablel[data-v-4d12265a]) {
    margin: 10px 20px 20px 20px;
}

:deep(.el-form-item) {
    margin-bottom: 20px;
}

:deep(.el-input__inner) {
    margin-top: 10px;
}

:deep(.el-textarea__inner) {
    width: 350px;
}

/* 课程图片 */
.kechengbox {
    width: 260px;
    height: 240px;
    border: 1px solid #E7E8E8;
    display: inline-block;
    position: relative;
}

.kechengbox {
    text-align: center;
}

.Deleteimg {
    cursor: pointer;
}

/* 上传 */
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.prompt {
    width: 500px;
    height: 30px;
    /* border: 1px solid red; */
    position: absolute;
    margin-left: 260px;
    margin-top: -25px;
    color: #8c939d;

}

element.style {
    width: 100px;
}

.content {
    width: 600px;
    margin: 0 auto;
}

/* 返回 */
.fanhui {
    float: right;
    width: 70px;
    margin-right: 40px;
}

.box {
    overflow: hidden;
}

.content {
    padding-top: 10px;
}

.head {
    height: 50px;
    background-color: rgb(228, 228, 228);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 10px 10px 0 0;
}
</style>